/**
 * 缓存管理器
 *
 * example 获取缓存类型列表
 * let res = await getDictTypeList()
 * console.log(res);
 *
 * example 获取设备模型缓存
 * let res = await getDictList(DictType.DeviceMode);
 * console.log(res);
 */
import * as dictApi from "@/api/modules/system/dict";
import { SYSTEM_ID } from "@/config/config";
const db = sessionStorage;
export interface Dict {
	type: string;
	label: string;
	key: any;
}
export enum bizDictType {
	//变量
	variable = "variable",
	//公共
	public = "public"
}
export enum DictType {
	//服务状态
	ServiceStatus = "service_status",
	//设备模式
	DeviceMode = "sys_device_mode",
	//设备模式
	AppletFrom = "applet_from",
	//设备模式
	ModbusType = "modbus_type",
	//设备模式
	UploadMode = "upload_mode",
	//应用类型
	AppletType = "applet_type",
	//应用运行系统
	AppletPlatform = "applet_platform",
	AlarmType = "alarm_type",
	//AI事件類型
	AiEvent = "airoom_ai_event"
}

const CacheKeys = {
	dictType: "dictType",
	dict: "dict"
};

export const cleanDictCache = async () => {
	let res = db.getItem(CacheKeys.dictType);
	let typeList = [];
	if (res) {
		typeList = JSON.parse(res);
	}

	//删除数据字典
	for (let type in typeList) {
		db.removeItem(CacheKeys.dict + "_" + type);
	}
	//删除字典类型
	db.removeItem(CacheKeys.dictType);
};

/**
 * 获取字典类型列表
 */
export const getDictTypeList = async (): Promise<{ [key: string]: Dict }> => {
	//获取缓存
	let typeList = db.getItem(CacheKeys.dictType);
	if (typeList) {
		return JSON.parse(typeList);
	}

	//获取后台数据
	let { result } = await dictApi.getDictTypeList({ pageIndex: 1, pageSize: 999 });

	//转换到缓存
	let data: { [key: string]: Dict } = {};
	(result.records || []).map((item: any) => {
		data[item.dictType] = {
			type: "",
			label: item.dictName,
			key: item.dictType
		};
	});
	db.setItem(CacheKeys.dictType, JSON.stringify(data));
	return getDictTypeList();
};

/**
 * 获取字典类型详情
 * @param key
 * @returns
 */
export const getDictTypeInfo = async (key: string): Promise<Dict> => {
	let res = await getDictTypeList();
	return res[key];
};

/**
 * 获取字典数据列表
 * @param key
 * @returns
 */
export const getDictList = async (type: string, refresh = false): Promise<Dict[]> => {
	let dictListKey = CacheKeys.dict + "_" + type;
	//获取缓存
	let typeList = db.getItem(dictListKey);
	if (typeList && !refresh) {
		return JSON.parse(typeList)[type];
	}

	//获取后台数据
	let { result } = await dictApi.getDictList({ dictType: type, pageIndex: 1, pageSize: 999, systemId: SYSTEM_ID });

	//转换到缓存
	let dictList: Dict[] = (result.records || []).map((item: any) => {
		return {
			type: item.dictType,
			label: item.dictLabel,
			key: JSON.parse(item.dictValue)?.value
		};
	});
	let data: { [key: string]: Dict[] } = {};
	data[type] = dictList;
	db.setItem(dictListKey, JSON.stringify(data));
	return getDictList(type);
};

export const getDictInfo = async (type: string, key: any, defaultInfo?: boolean): Promise<Dict | undefined> => {
	let list: Dict[] = await getDictList(type);
	let info = list.find(item => item.key == key);
	if (info) {
		return info;
	} else if (defaultInfo) {
		return list[0];
	} else {
		return undefined;
	}
};
export const getDictObj = async (type: string) => {
	let list = await getDictList(type);
	let obj: any = {};
	list.map((item: any) => {
		obj[item.key] = item.label;
	});
	return obj;
};
